package com.ruanjian.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruanjian.commonUtils.R;
import com.ruanjian.eduservice.entity.EduTeacher;
import com.ruanjian.eduservice.vo.teacherQuery;
import com.ruanjian.eduservice.service.EduTeacherService;
import com.ruanjian.serviceBase.MyException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-11-14
 */
@Api(description = "讲师管理接口")
@RestController
@RequestMapping("/eduservice/edu-teacher")
@CrossOrigin
public class EduTeacherController {
    @Autowired
    EduTeacherService eduTeacherService;

    @ApiOperation(value = "查询所有讲师")
    //查询所有讲师
    @GetMapping("/findAll")
    public R findAllTeacher()
    {


        List<EduTeacher> list = eduTeacherService.list(null);
       return R.ok().data("1",list);
    }





    @ApiOperation(value = "根据id删除讲师")
    //根据id删除讲师
    @DeleteMapping("{id}")
    public R removeTeacher(@ApiParam(value = "讲师id",required = true) @PathVariable("id") String d)
    {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("id",d);
        boolean remove = eduTeacherService.remove(queryWrapper);
        if(remove)
        {
            return R.ok();
        }else
        {
            return R.error();
        }
    }

    //分页查询
    @ApiOperation("分页查询")
    @GetMapping("pageTeacher/{current}/{limit}")
    public R pageListTeacher(@ApiParam(value = "当前页")@PathVariable("current") long current,@PathVariable("limit") long limit)
    {
        Page<EduTeacher> page=new Page<>(current,limit);
        IPage<EduTeacher> page1 = eduTeacherService.page(page, null);
        //总记录数
        long total = page1.getTotal();

        //当前页的数据
        List<EduTeacher> records = page1.getRecords();

        return R.ok().data("total",total).data("rows",records);
    }


    @ApiOperation("条件查询")
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition( @PathVariable("current") Long current, @PathVariable("limit") Long limit,@RequestBody(required = false) teacherQuery  tquery)
    {
        Page<EduTeacher> page=new Page<>(current,limit);
        QueryWrapper<EduTeacher> queryWrapper=new QueryWrapper<>();
        //判断条件是否为空
        String name = tquery.getName();
        String level = tquery.getLevel();
        String begin = tquery.getBegin();
        String end = tquery.getEnd();

        if(!StringUtils.isEmpty(name))
        {
            queryWrapper.like("name",name);
        }
        if(!StringUtils.isEmpty(level))
        {
            queryWrapper.eq("level",level);
        }
        if(!StringUtils.isEmpty(begin))
        {
            queryWrapper.ge("gmt_create",begin);
        }
        if(!StringUtils.isEmpty(end))
        {
            queryWrapper.le("gmt_create",end);
        }
        queryWrapper.orderByDesc("gmt_create");
        //实现条件查询分页
        eduTeacherService.page(page,queryWrapper);
        long total = page.getTotal();
        List<EduTeacher> records=page.getRecords();
        return R.ok().data("total",total).data("rows",records);

    }


    @ApiOperation("增加讲师")
    @PostMapping("/addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher)
    {
        boolean save = eduTeacherService.save(eduTeacher);
        if(save)
        {
            return  R.ok();
        }else
        {
            return  R.error();
        }
    }

    @ApiOperation("根据id查询老师")
    @GetMapping("/getTeacher/{id}")
    public R getTeacher(@PathVariable("id") String id)
    {
        EduTeacher byId = eduTeacherService.getById(id);
        return R.ok().data("teacher",byId);
    }


    @ApiOperation("修改讲师")
    @PostMapping("/updateTeacher")
    public  R updateTeacher(@RequestBody EduTeacher eduTeacher)
    {
        boolean update = eduTeacherService.updateById(eduTeacher);
        if(update)
        {

            return R.ok();
        }else
        {
            return  R.error();
        }
    }



}

